﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script type="text/javascript">
    $(function() {
        var select = $("#employee");
        getUser(select);
    });

    function getUser(tag) {
        var userId = $(tag).attr("value");
        $.ajax({
            type: "POST",
            url: "/Authority/GetUser",
            data: { id: userId },
            success: function(data) {
                $("#usercarre").html(data);
            }
        });
    }

    function changeAuth() {
        var userId = $("#employee").attr("value");
        var authId = $("#carrers").attr("value");


        $.ajax({
            type: "POST",
            url: "/Authority/changeAuth",
            data: { "userId": userId, "authId": authId },
            success: function(data) {
                $("#usercarre").html(data);
            }
        })
    }

    var authNodes;
    var authsetting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    $(document).ready(function() {
        //authNodes = $("#authnode").val();
        //$.fn.zTree.init($("#authTree"), authsetting, eval(authNodes));

        //        $("#authLand").slideToggle("slow");
        //        $("#authLand").css("visibility", "hidden");
    });

    var persetting = {};

    function getPerszTree(tag) {
        var authId = $(tag).attr("value");

        var authName = $("#carrers").find("option:selected").text();

        $.ajax({
            type: "POST",
            url: "/Authority/RefreshPerszTree",
            data: { "authId": authId },
            success: function(data) {
                //$("#authTypeShow").html("'" + authName + "'&nbsp所有权限");

                $.fn.zTree.init($("#authTree"), authsetting, eval(data));
            }
        })
    }

    function addAuth() {
        var treeObj = $.fn.zTree.getZTreeObj("authTree");
        var nodes = treeObj.getNodes();
        var chilmenu = '';
        var menu = '';
        $.each(nodes, function(key, node) {
            var id = $(node).attr("id");
            var check = node.checked;
            var chilNodes = node.children;
            $.each(chilNodes, function(chilKey, chilNode) {
                var chilId = $(chilNode).attr("id");
                var chilCheck = chilNode.checked;
                if (chilCheck)
                    chilmenu += chilId + ',';
            });
            if (chilmenu != "") {
                chilmenu = chilmenu.substring(0, chilmenu.length - 1);
                menu += id + ',' + chilmenu + ";";
            } else {
                return true;
            }
            chilmenu = '';
        });
        var authName = $("#typeName").val();

        $.ajax({
            type: "POST",
            url: "/Authority/AddAuth",
            data: { "authName": authName, "authority": menu },
            success: function(data) {
                $("#workarea-9-1").html(data);
            }
        });
    }

    function changeAuth() {
        var zTree = $.fn.zTree.getZTreeObj("authTree");
        var nodes = zTree.getCheckedNodes(true);
        var rootlist = new Array();
        for (var i = 0, l = nodes.length; i < l; i++) {
            //alert(nodes[i].level + " " + nodes[i].tag);
            if (nodes[i].level == 0) {
                rootlist.push(nodes[i]);
            }
        }
        //alert(rootlist.length);
        var resstr = "";
        for (var i = 0; i < rootlist.length; i++) {
            if (i > 0) {
                resstr += ";";
            }
            var rootnode = rootlist[i];
            resstr += rootnode.id;
            resstr += ",";
            for (var j = 0; j < nodes.length; j++) {
                var curnode = nodes[j];
                if (curnode.tag == rootnode.id) {
                    resstr += curnode.id;
                    resstr += ",";
                }
            }
        }
        //alert(resstr);
        if (resstr != "") {

            var authId = $("#carrers").attr("value");

            $.ajax({
                type: "POST",
                url: "/Authority/AuthChange",
                data: { "authId": authId, "authority": resstr },
                success: function(restag) {
                    if (restag.res) {
                        alert("操作成功");
                    }
                    else {
                        alert("操作失败");
                    }
                }
            });
        }
    }

    //    function visitAuth() {
    //        $("#authLand").css("visibility", "visible");
    //        $("#authLand").slideToggle("slow");
    //    }

</script>

<div style="padding-top: 40px; padding-left: 10px; width: 1090px;">
    <div class="leftLand" style="width: 270px;">
        <span style="padding-left: 25px;">员工:</span><%=Html.DropDownList("employee", null, new { onchange = "getUser(this)" ,style = "width:150px; margin-left:8px; border:solid 1px #6C92AD;" })%><br />
        <p style="margin: 10px 0;">
            对应角色:<span id="usercarre" style="margin-left: 8px;"></span></p>
        <p style="margin: 10px 0;">
            更改角色:<%=Html.DropDownList("carrers", null, new { onchange = "getPerszTree(this);", style = "width:150px; margin-left:8px; border:solid 1px #6C92AD;" })%></p>
        <input type="button" class="btn_style" onclick="changeAuth();" value="确认" /><br />
        <div id="authTypeShow">
        </div>
        <div id="persTree" class="ztree">
        </div>
        <input id="persnode" type="hidden" />
    </div>
    <div class="rightLand">
        <%-- <input type="button" onclick="visitAuth()" value="添加权限类型" />--%>
        <div id="authLand" style="border-left: 1px solid #9A9A9A; padding-left: 30px;">
            <label>
                对应功能:</label>
            <%--<input id="typeName" type="text" style="width: 150px; margin-left: 8px;
                    border: solid 1px #6C92AD;" />
           <input type="button" class="btn_style" onclick="addAuth()" value="添加权限类型" />--%>
            <div id="authTree" class="ztree">
            </div>
            <input id="authnode" type="hidden" value="<%=ViewData["menuzTree"] %>" />
            <input type="button" class="btn_style" onclick="changeAuth()" value="确认" />
        </div>
    </div>
</div>
</div> 